XML을 DB로 변환, XMLtoDB.java :: JSP 일반[SSISO Community]
 
SSISO 카페 SSISO Source SSISO 구직 SSISO 쇼핑몰 SSISO 맛집
추천검색어 : JUnit   Log4j   ajax   spring   struts   struts-config.xml   Synchronized   책정보   Ajax 마스터하기   우측부분

JSP 일반
[1]
등록일:2008-11-10 20:10:59 (0%)
작성자:
제목:XML을 DB로 변환, XMLtoDB.java
[01]  XML을  DB로  변환

1.  XML  데이터  파일

DELETE  FROM  office;

COMMIT;

SELECT  officenum,  company,  area,  rent  FROM  office;

  


>>>>>  WebContent/xml/office/office_Y2007M7D25_H8M50S29.xml

<?xml  version="1.0"  encoding="euc-kr"?>
<officeList>
    <office>
        <officenum>5</officenum>
        <company>(주)IT</company>
        <area>200</area>
        <rent>10000000</rent>
    </office>
    <office>
        <officenum>6</officenum>
        <company>(주)WEB</company>
        <area>200</area>
        <rent>10000000</rent>
    </office>
    <office>
        <officenum>7</officenum>
        <company>(주)SI</company>
        <area>200</area>
        <rent>10000000</rent>
    </office>
</officeList>

  

  

2.  Manager  Class

>>>>>  XMLtoDB.java

package  www.xml;

import  java.io.File;
import  java.io.FileWriter;
import  java.sql.Connection;
import  java.sql.PreparedStatement;
import  java.sql.ResultSet;
import  java.sql.Statement;
import  java.util.Iterator;
import  java.util.List;
import  java.util.Vector;

import  org.jdom.Document;
import  org.jdom.Element;
import  org.jdom.input.SAXBuilder;
import  org.jdom.output.Format;
import  org.jdom.output.XMLOutputter;

import  www.utility.ConnectionPoolMgr;

//XML  -->  DB
public  class  XMLtoDB  {
        
        /**  Connection  Pool  */
        private  ConnectionPoolMgr  dbconnect=null;
        
        private  String  xml_dir;
        private  String  xml_name;
        
        SAXBuilder  builder;
        Document  doc;
        Element  root;

        public  XMLtoDB(){
                xml_dir  =  "F:/200708_ojt1330/eclipse/workspace/www_pilot/WebContent/xml/office/";
                xml_name=  "office.xml";

                try  {
                        builder  =  new  SAXBuilder();
                        doc  =  builder.build(new  File(xml_dir,  xml_name));
                        dbconnect  =  new  ConnectionPoolMgr();    //접속  개체  생성
                }catch(Exception  ex)  {
                        ex.printStackTrace();
                }

        }

        public  XMLtoDB(int  DBCPMode){
                xml_dir  =  "F:/200708_ojt1330/eclipse/workspace/www_pilot/WebContent/xml/office/";
                xml_name=  "office.xml";

                try  {
                        builder  =  new  SAXBuilder();
                        doc  =  builder.build(new  File(xml_dir,  xml_name));
                        dbconnect  =  new  ConnectionPoolMgr(DBCPMode);    //접속  개체  생성
                }catch(Exception  ex)  {
                        ex.printStackTrace();
                }

        }

        public  XMLtoDB(int  DBCPMode,  String  xml_name){
                xml_dir  =  "F:/200708_ojt1330/eclipse/workspace/www_pilot/WebContent/xml/office/";
                this.xml_name  =  xml_name;

                try  {
                        builder  =  new  SAXBuilder();
                        doc  =  builder.build(new  File(xml_dir,  xml_name));
                        dbconnect  =  new  ConnectionPoolMgr(DBCPMode);    //접속  개체  생성
                }catch(Exception  ex)  {
                        ex.printStackTrace();
                }

        }        
        
        /**
          *  officelist태그  기준으로  offcie  태그를  읽어  Vector형태로  리턴
          *    
          *  <officeList>
          *          <office>
          *                  <officenum></officenum>
          *                  <company></company>
          *                  <area></area>
          *                  <rent></rent>
          *          </office>
          *          <office>
          *                  <officenum></officenum>
          *                  <company></company>
          *                  <area></area>
          *                  <rent></rent>
          *          </office>
          *  </officeList>                
          */
        private  Vector  XMLtoObject(){
                List  list  =  null;
                Vector  dataList  =  new  Vector();
                
                try{
                        //  officeList  root태그  산출
                        root  =  doc.getRootElement();  
                        
                        //  여러개의  Address태그를  읽어  옵니다.
                        list=root.getChildren("office");
                        
                        Iterator  it  =  list.iterator();

                        while  (  it.hasNext()){
                                Element  element  =  (Element)it.next();
                                
                                OfficeDTO  officeDTO  =  new  OfficeDTO();
                                int  officenum  =  Integer.parseInt(element.getChild("officenum").getText());
                                String  company  =  element.getChild("company").getText();
                                int  area  =  Integer.parseInt(element.getChild("area").getText());
                                int  rent  =  Integer.parseInt(element.getChild("rent").getText());
                                
                                officeDTO.setOfficenum(officenum);
                                officeDTO.setCompany(company);
                                officeDTO.setArea(area);
                                officeDTO.setRent(rent);
                                
                                dataList.add(officeDTO);  //Vector  에  저장
                        }
                        
                }catch(Exception  e){
                        e.printStackTrace();
                }
                return  dataList;
        }
        
        //  Vector를  DB로  변환
        private  void  ObjecttoDB(Vector  dataList){
                Connection  con  =  null;
                PreparedStatement  pstmt  =  null;
                ResultSet  rs  =  null;
                StringBuffer  sql=null;

                Iterator  it  =  dataList.iterator();
                
                try{
                        con  =  dbconnect.getConnection();  //데이터베이스  접속                        
                        sql  =  new  StringBuffer();
                        sql.append("  INSERT  INTO  office(officenum,  company,  area,  rent)");
                        sql.append("  VALUES(office_officenum.nextval,?,?,?)");
                
                        pstmt  =  con.prepareStatement(sql.toString());
                        
                        while  (it.hasNext()){
                                OfficeDTO  officeDTO  =  (OfficeDTO)it.next();
                                
                                pstmt.setString(1,  officeDTO.getCompany());
                                pstmt.setInt(2,  officeDTO.getArea());
                                pstmt.setInt(3,  officeDTO.getRent());
                                
                                pstmt.executeUpdate();
                        }
                }catch(Exception  e){
                        e.printStackTrace();
                }
        }
        
        //XML을  DB로  변환하는  메소드  호출,  XML  --->  DTO  --->  Vector  -->  DB
        public  void  convertXMLtoDB(){
                //DB에서  레코드  추출
                try{
                        Vector  dataList  =  XMLtoObject();
                        ObjecttoDB(dataList);
                }catch(Exception  e){
                        System.out.println(e);
                }
                System.out.println("DB로  변환을  성공했습니다.");
        }        
}

  

  

3.  Test

>>>>>  XMLtoDB_Test.java

package  test.www.xml;

import  www.xml.XMLtoDB;

public  class  XMLtoDB_convertXMLtoDB  {

        public  static  void  main(String[]  args)  {
                XMLtoDB  mgr  =  new  XMLtoDB(1,  "office_Y2007M7D25_H8M50S29.xml");
                mgr.convertXMLtoDB();

        }
}

  
[출처]  [24-D12][XML]  XML을  DB로  변환,  XMLtoDB.java|작성자  푸우하하
http://blog.naver.com/poohhahahaha/10024310965
[본문링크] XML을 DB로 변환, XMLtoDB.java
[1]
코멘트(이글의 트랙백 주소:/cafe/tb_receive.php?no=31410
작성자
비밀번호

 

SSISOCommunity

[이전]

Copyright byCopyright ⓒ2005, SSISO Community All Rights Reserved.